﻿using MovingChame.Web.Helpers;
using MovingChame.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MovingChame.Web.Infrastructure
{
    public class FriendProvider : IFriendProvider
    {
        private static FriendProvider current;
        public static FriendProvider Current
        {
            get
            {
                if (current == null) current = new FriendProvider();
                return current;
            }
        }

        #region Configuration
        private string applicationName;
        public string ApplicationName
        {
            get { return applicationName; }
            set { applicationName = value; }
        }

        private string connectionStringName;
        private string providerName;
        private MovingChameModelContainer db;

        public void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            providerName = name;

            if (config["applicationName"] != null)
                applicationName = config["applicationName"];

            if (config["connectionStringName"] != null)
            {
                connectionStringName = config["connectionStringName"];
                db = new MovingChameModelContainer();
                db.Database.Connection.ConnectionString = ConfigurationHelper.GetConnectionString(connectionStringName);
            }
            else
                db = new MovingChameModelContainer();
        }

        private FriendProvider()
        {
            Initialize("FriendProvider", new System.Collections.Specialized.NameValueCollection());
        }
        #endregion

        #region Private Methods
        private bool RemoveFriendRecord(int userId, int userId1)
        {
            var friend = db.Friends.Where(o => o.UserId == userId).SingleOrDefault(o => o.UserId1 == userId1);
            if (friend == null)
                return false;
            db.Friends.Remove(friend);
            db.SaveChanges();
            return true;
        }
        #endregion

        #region Interface Methods
        public int AddFriend(int userId, int user1Id)
        {
            var newFriend = new Friend {
                UserId = userId,
                UserId1 = user1Id,
                FriendStatus = FriendStatus.Requested
            };

            db.Friends.Add(newFriend);
            db.SaveChanges();
            return newFriend.Id;
        }

        public bool ConfirmFriend(int userId, int user1Id)
        {
            var friend = db.Friends.Where(o => o.UserId == userId).SingleOrDefault(o => o.UserId1 == user1Id);
            if (friend == null)
            {
                var newFriend = new Friend
                {
                    UserId = userId,
                    UserId1 = user1Id,
                    FriendStatus = FriendStatus.Confirmed
                };

                db.Friends.Add(newFriend);

                var confirmFriend = db.Friends.Where(o => o.UserId == user1Id).SingleOrDefault(o => o.UserId1 == userId);
                if (confirmFriend == null)
                    return false;
                confirmFriend.FriendStatus = FriendStatus.Confirmed;

                db.SaveChanges();
                return true;
            }
            else
            {
                friend.FriendStatus = FriendStatus.Confirmed;

                var newFriend = new Friend
                {
                    UserId = user1Id,
                    UserId1 = userId,
                    FriendStatus = FriendStatus.Confirmed
                };

                db.Friends.Add(newFriend);
                db.SaveChanges();
                return true;
            }
        }

        public bool Unfriend(int userId, int userId1)
        {
            return (RemoveFriendRecord(userId, userId1) & RemoveFriendRecord(userId1, userId));
        }

        public bool CancleFriend(int userId, int userId1)
        {
            return RemoveFriendRecord(userId, userId1);
        }

        public List<Friend> GetFriends(int userId)
        {
            return db.Friends.Where(o => o.UserId == userId).Where(o => o.FriendStatus == FriendStatus.Confirmed).ToList();
        }

        public List<Friend> GetRequestedFriends(int userId)
        {
            return db.Friends.Where(o => o.UserId1 == userId).Where(o => o.FriendStatus == FriendStatus.Requested).ToList();
        }

        public List<Friend> GetRequestingFriends(int userId)
        {
            return db.Friends.Where(o => o.UserId == userId).Where(o => o.FriendStatus == FriendStatus.Requested).ToList();
        }
        #endregion
    }
}